package com.example.myapp

import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.example.myapp.databinding.ActivityNativeBinding
import com.example.myapp.router.PageRouter
import com.example.myapp.router.RouterKey
import com.idlefish.flutterboost.containers.BoostFlutterActivity

/**
 * @author weng
 * @date 2022/7/25 13:58
 * @describe
 */
class AndroidNativeActivity : AppCompatActivity() {

    private var activityNativeBinding:ActivityNativeBinding? =null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityNativeBinding = ActivityNativeBinding.inflate(layoutInflater)
        setContentView(activityNativeBinding?.root)
        supportActionBar?.title = "AndroidActivity"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        if (intent.extras != null) {
            val requestParams =
                (intent.extras?.get(PageRouter.EXTRA_PARAMS) as BoostFlutterActivity.SerializableMap).map
            val text = requestParams[RouterKey.Test.HasRequestParams.EXTRA_KEY_NATIVE].toString()
            activityNativeBinding?.tvContent?.text = String.format("This is flutter page request params：\n$text")

        }

        activityNativeBinding?.btnClose?.setOnClickListener {
            val params = mutableMapOf<String, Any>()
            params[RouterKey.Test.HasResultActivity.EXTRA_KEY_AGE] = 18
            params[RouterKey.Test.HasResultActivity.EXTRA_KEY_NAME] = "陆先生"
            PageRouter.finishAndResult(this, params)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}